package com.acp.service.impl;

import com.acp.context.Config;
import com.acp.dao.acpo.DcsCollectCodeMapper;
import com.acp.dao.acpo.DcsTaskPerformanceMapper;
import com.acp.model.DcsCollectCode;
import com.acp.model.DcsTaskPerformance;
import com.acp.param.DcsCollectCodeParam;
import com.acp.result.PageResult;
import com.acp.result.ResultInfo;
import com.acp.result.ResultUtil;
import com.acp.service.DcscollectCodeService;
import com.acp.util.UUIDBuild;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.controller.construction
 * @Description: 采集编号管理实现类
 * @author: 张星亮
 * @date: 2019/4/5
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class DcsCollectCodeServiceImpl implements DcscollectCodeService {

    @Resource
    private DcsCollectCodeMapper dcsCollectCodeMapper;

    @Resource
    private DcsTaskPerformanceMapper dcsTaskPerformanceMapper;

    /**
     * 添加
     * @param dcsCollectCodeParam
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public ResultInfo addDcsTask(DcsCollectCodeParam dcsCollectCodeParam) {
        if (dcsCollectCodeParam.collect_name_code.size() != 0) {
            String taskcode = dcsCollectCodeParam.getTask_code();
            int b = dcsCollectCodeMapper.selectCode(taskcode);
            if (b > 0) {
                return ResultUtil.createWarning(Config.MESSAGE, 912, null);
            }
            for (int a = 0; a < dcsCollectCodeParam.collect_name_code.size(); a++) {
                List<String> list = dcsCollectCodeParam.collect_name_code;
                String code = list.get(a);
                DcsCollectCode dcsCollectCodeParam1 = DcsCollectCode.builder().id(UUIDBuild.getUUID()).group_code(dcsCollectCodeParam.getGroup_code()).collect_code(code).task_code(dcsCollectCodeParam.getTask_code()).street(dcsCollectCodeParam.getStreet()).community(dcsCollectCodeParam.getCommunity()).build();
                int i = dcsCollectCodeMapper.insert(dcsCollectCodeParam1);
            }
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        } else {
            return ResultUtil.createWarning(Config.MESSAGE, 911, null);
        }
    }

    /**
     * 删除
     * @param list
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public ResultInfo deleteDcsTaskById(List<DcsCollectCodeParam> list) {
        if (list != null && list.size() != 0) {
            for (int a = 0; a < list.size(); a++) {
                DcsCollectCodeParam dcsCollectCodeParam = list.get(a);
                String code = dcsCollectCodeParam.getTask_code();
                int i = dcsCollectCodeMapper.deleteByPrimaryKey(code);
            }
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }

    /**
     * 修改
     * @param dcsCollectCodeParam
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public ResultInfo editDcsTask(DcsCollectCodeParam dcsCollectCodeParam) {
        if (dcsCollectCodeParam.collect_name_code.size() != 0) {
            String t_code = dcsCollectCodeParam.getTask_code();
            int n = dcsCollectCodeMapper.deleteByPrimaryKey(t_code);
            for (int a = 0; a < dcsCollectCodeParam.collect_name_code.size(); a++) {
                List<String> list = dcsCollectCodeParam.collect_name_code;
                String code1 = list.get(a);
                DcsCollectCode dcsCollectCodeParam1 = DcsCollectCode.builder().id(UUIDBuild.getUUID()).group_code(dcsCollectCodeParam.getGroup_code()).street(dcsCollectCodeParam.getStreet()).community(dcsCollectCodeParam.getCommunity()).collect_code(code1).task_code(dcsCollectCodeParam.getTask_code()).build();
                int i = dcsCollectCodeMapper.insert(dcsCollectCodeParam1);
            }
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }

    /**
     * 查询所有编码
     * @return 返回PageResult 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public PageResult queryCodeAll() {
        List<DcsCollectCode> dcsCollectCodes = dcsCollectCodeMapper.selectCodeAll();
        PageInfo<DcsCollectCode> pageInfo = new PageInfo<>(dcsCollectCodes);
        Long count = pageInfo.getTotal();
        return PageResult.success(dcsCollectCodes, count);
    }
}
